package com.tang.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tang.reggie.common.BaseContext;
import com.tang.reggie.common.R;
import com.tang.reggie.pojo.ShoppingCart;
import com.tang.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 购物车的添加
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R save(@RequestBody ShoppingCart shoppingCart){
        log.info("购物车模块：购物车添加功能");
        ShoppingCart shoppingCart1=shoppingCartService.add(shoppingCart);
        return R.success(shoppingCart1);//返回添加的数据，主要是返回给前端数量，让前端可以看见数量
    }

    /**
     * 购物车的展示
     * @return
     */
    @GetMapping("/list")
    public R list(){
        log.info("购物车模块：查看购物车功能");
        List<ShoppingCart> shoppingCartList=shoppingCartService.getAll(BaseContext.getCurrentId());
        return R.success(shoppingCartList);
    }

    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    public R clean(){
        log.info("购物车模块：清空购物车功能");
        shoppingCartService.clean(BaseContext.getCurrentId());
        return R.success("清空购物车成功");
    }

    /**
     * 购物车的删除
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public R sub(@RequestBody ShoppingCart shoppingCart){
        log.info("购物车模块：删除购物车功能");
        ShoppingCart shoppingCart1=shoppingCartService.sub(shoppingCart);
        return R.success(shoppingCart1);
    }

}
